const Controller = require('egg').Controller;
const sqlConfig = require('../util/sqlConfig')
const err  = require('../util/err')
const moment = require('moment')

class BusinessController extends Controller {
    //  商家列表
    async businessList() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        const mysql = app.mysql
        if(r_body){
            const list = await mysql.query(sqlConfig.businessList(r_body)).catch(err=>{
                ctx.body = err.err3(err)
            })
            ctx.body = {
                code: 200,
                msg: 'SUCCESS',
                data: {
                   list:list
                }
            }
        }else{
            ctx.body = err.err1
        }
        
    }

    // 商家列表详情
    async businessDetail() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        const mysql = app.mysql
        if(r_body.id){
            const _data = await mysql.query(sqlConfig.businessDetail(r_body)).catch(err=>{
                ctx.body = err.err3(err)
            })
            ctx.body = {
                code: 200,
                msg: 'SUCCESS',
                data: _data.length?_data[0]:{}
            }
        }else{
            ctx.body = err.err1
        }
    }

    // 活动列表
    async activeList() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        const mysql = app.mysql
        if(r_body){
            const list = await mysql.query(sqlConfig.activeList(r_body)).catch(err=>{
                ctx.body = err.err3(err)
            })
            ctx.body = {
                code: 200,
                msg: 'SUCCESS',
                data: {
                   list:list
                }
            }
        }else{
            ctx.body = err.err1
        }
    }

    // 活动列表详情
    async activeDetail() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        const mysql = app.mysql
        if(r_body.id){
            const _data = await mysql.query(sqlConfig.activeDetail(r_body)).catch(err=>{
                ctx.body = err.err3(err)
            })
            ctx.body = {
                code: 200,
                msg: 'SUCCESS',
                data: _data.length?_data[0]:{}
            }
        }else{
            ctx.body = err.err1
        }
    }

    // 咨讯列表
    async newsList() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        const mysql = app.mysql
        if(r_body){
            const list = await mysql.query(sqlConfig.newsList(r_body)).catch(err=>{
                ctx.body = err.err3(err)
            })
            ctx.body = {
                code: 200,
                msg: 'SUCCESS',
                data: {
                   list:list
                }
            }
        }else{
            ctx.body = err.err1
        }
    }

    // 咨讯列表详情
    async newsDetail() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        const mysql = app.mysql
        if(r_body.id){
            const _data = await mysql.query(sqlConfig.newsDetail(r_body)).catch(err=>{
                ctx.body = err.err3(err)
            })
            ctx.body = {
                code: 200,
                msg: 'SUCCESS',
                data: _data.length?_data[0]:{}
            }
        }else{
            ctx.body = err.err1
        }
    }

    // 商户入驻
    async businessInsert() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        const mysql = app.mysql
        if(r_body.name&&r_body.tel&&r_body.businessName&&r_body.address){
            const result = await this.app.mysql.insert('info', {
                name:r_body.name,
                tel:r_body.tel,
                businessName:r_body.businessName,
                address:r_body.address,
                uSchoolId:r_body.uSchoolId
            })
            if(result.affectedRows === 1){
                ctx.body = {
                    code: 200,
                    msg: 'SUCCESS',
                    data: {}
                }
            }else{
                ctx.body = err.err4
            }
            
        }else{
            ctx.body = err.err1
        }
    }

    // 获取首页信息
    async indexInfo() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        const mysql = app.mysql
        const data =  await mysql.select('banner')
        ctx.body = {
            code: 200,
            msg: 'SUCCESS',
            data: data.length?data[0]:{}
        }
    }
    // 获取校园管理者联系信息
    async concatInfo() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        const mysql = app.mysql
        const {uSchoolId} = r_body
        const data =  await mysql.select('concat',{
            where:{
                uSchoolId
            }
        })
        ctx.body = {
            code: 200,
            msg: 'SUCCESS',
            data: data.length?data[0]:{}
        }
    }

    // 领券
    async getCard() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        const mysql = app.mysql
        if(r_body.loginId&&r_body.manageId&&r_body.businessId&&r_body.type&&r_body.cont){
            const data =  await this.app.mysql.select('card',{
                where: { 
                    loginId:r_body.loginId,
                    manageId:r_body.manageId,
                    businessId:r_body.businessId,
                    type:r_body.type,
                }
            })
            // 同类型券未领取过
            if(!data.length){
                const result = await this.app.mysql.insert('card', {
                    loginId:r_body.loginId,
                    manageId:r_body.manageId,
                    businessId:r_body.businessId,
                    type:r_body.type,
                    cont:r_body.cont,
                })
                if(result.affectedRows === 1){
                    ctx.body = {
                        code: 200,
                        msg: 'SUCCESS',
                        data: {}
                    }
                }else{
                    ctx.body = err.err4
                }
            }else{
                const on = data.some(item=>item.isUse===0)
                if(on){
                    // 已领取未使用
                    ctx.body = err.err6
                }else{
                    // 已领取已使用
                    const result = await this.app.mysql.insert('card', {
                        loginId:r_body.loginId,
                        manageId:r_body.manageId,
                        businessId:r_body.businessId,
                        type:r_body.type,
                        cont:r_body.cont,
                    })
                    if(result.affectedRows === 1){
                        ctx.body = {
                            code: 200,
                            msg: 'SUCCESS',
                            data: {}
                        }
                    }else{
                        ctx.body = err.err4
                    }
                }
                
            }
        }else{
            ctx.body = err.err1
        }
    }
    // 券列表
    async cardList() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        if(r_body.loginId){
            let obj
            // 查所有
            if(r_body.isUse===2){
                obj = {
                    loginId: r_body.loginId,
                }
            }else{
            // 0 未使用 1 已使用
                obj = {
                    loginId: r_body.loginId,
                    isUse:r_body.isUse
                }
            }
            const data = await app.mysql.select('card', { 
                where: obj, 
                orders: [['id','desc']], 
              });
            const outData = [...data]
            for(let i = 0; i<outData.length;i++){
                const _data = await app.mysql.select('business', { 
                    where: {
                        id:outData[i].businessId
                    }
                  })
                  outData[i].title = _data[0].title
                  outData[i].publishTime = _data[0].publishTime
                  outData[i].endTime = _data[0].endTime
            }
            ctx.body = {
                code: 200,
                msg: '查询成功',
                data: data
            }
        }else{
            ctx.body = err.err1
        }
    }
    // 使用券
    async useCard() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        if(r_body.id){
            await app.mysql.update('card', {
                isUse:1,
                useTime:moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
            },{
                where: {
                    id: r_body.id
                }
            })
            ctx.body = {
                code: 200,
                msg: '使用成功',
                data: {}
            }
        }else{
            ctx.body = err.err1
        }
    }
    // 删除券
    async cardDelete() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        if(r_body.id){
            await app.mysql.delete('card', {
                id: r_body.id,
            });
            ctx.body = {
                code: 200,
                msg: '删除成功',
                data: {}
            }
        }else{
            ctx.body = err.err1
        }
    }
    // 增加券领取量
    async cardVisit() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        if(r_body.businessId){
            const data = await app.mysql.select('business',{
                where:{
                    id:r_body.businessId
                }
            })
            const visit = data[0].visit+1?data[0].visit+1:1
            await this.app.mysql.update('business',{
                visit:visit
            },{
                where:{
                    id:r_body.businessId
                }
            })
            ctx.body = {
                code: 200,
                msg: '增加成功',
                data: {}
            }
        }else{
            ctx.body = err.err1
        }
    }

}



module.exports = BusinessController;